package com.clstu.mhl.view;

import com.clstu.mhl.domain.*;
import com.clstu.mhl.service.*;

import java.util.List;
import java.util.Locale;
import java.util.Scanner;
import java.util.UUID;

/**
 * 主菜单界面(框架先搭好//写程序先搭好框架后面写起来思路更清晰)
 */
public class MHLView {
    public static void main(String[] args) {
        new MHLView().mainMeine();
    }
    //服务成员,调用来获得数据的
    Scanner scanner = new Scanner(System.in);
    EmployeeService employeeService = new EmployeeService();
    DiningTableService diningTableService = new DiningTableService();
    MenuService menuService = new MenuService();
    BillService billService = new BillService();
    MultiTableBeanService multiTableBeanService = new MultiTableBeanService();
    //显示餐桌信息
    public void showTables(){
        System.out.println("============餐桌状态============");
        System.out.println("\t\t桌号\t\t\t状态");
        List<DiningTable> tbs = diningTableService.listTable();
        for (DiningTable tb : tbs) {
            System.out.println(tb);
        }
        System.out.println("============显示完毕============");
    }
    //显示所有菜品
    public void showMenus(){
        System.out.println("=================所有菜品===============");
        System.out.println("编号\t\t\t名字\t\t\t\t类型\t\t价格");
        List<Menu> menus = menuService.getMenus();
        for (Menu menu : menus) {
            System.out.println(menu);
        }
    }
    //预定餐桌
    public void orderTable(){
        System.out.println("========预定餐桌===========");
        System.out.print("请输入想要预定的餐桌号:");
        String id = scanner.next();
        DiningTable tb = diningTableService.getById(id);
        if(tb==null){
            System.out.println("失败,该餐桌不存在..");
        }else {
            if(tb.getStatus().equals("空")){
                //确实是否真的需要订餐
                System.out.print("请确认是否需要订餐(y/n)(请输入):");
                char yn = scanner.next().toUpperCase(Locale.ROOT).charAt(0);
                if(yn=='Y') {
                    System.out.print("请输入订餐人姓名:");
                    String name = scanner.next();
                    System.out.print("还有电话:");
                    String phone = scanner.next();
                    System.out.println(diningTableService.setByIdNamePhone(id, name, phone) ? "预定成功!!" : "预定失败~~~");
                }else if(yn=='N') {
                    System.out.println("已取消~~~");
                }else {
                    System.out.println("输入有误..退出");
                }
            }else {
                System.out.println("失败,该餐桌已经有人了..");
            }
        }
    }
    //点菜
    public void orderMenu(){
        System.out.println("===========点菜=============");
        System.out.print("请输入您的餐桌号:");
        String tableId = scanner.next();
        DiningTable tb = diningTableService.getById(tableId);
        if(tb==null){
            System.out.println("失败,该餐桌不存在..");
        }else {
            if(!tb.getStatus().equals("空")){
                //确实是否真的需要订餐
                System.out.print("请确认是否需要点菜(y/n)(请输入):");
                char yn = scanner.next().toUpperCase(Locale.ROOT).charAt(0);
                if(yn=='Y') {
                    System.out.print("请输入菜号:");
                    String menuId = scanner.next();
                    if(menuService.getById(menuId)==null) {
                        System.out.println("失败,该菜品咱不存在,敬请期待..");
                        return;
                    }
                    System.out.print("需要多少份:");
                    String nums = scanner.next();
                    System.out.println(billService.addBillByXx(menuId,nums,tableId) ?
                            "点餐成功!!" : "点餐失败~~~");
                }else if(yn=='N') {
                    System.out.println("已取消~~~");
                }else {
                    System.out.println("输入有误..退出");
                }
            }else {
                System.out.println("失败,该餐桌未预定....");
            }
        }
    }
    //显示账单
    public void showBills(){
        System.out.println("==========账单如下=============");
        System.out.println("单号\t\t编号\t\t\t\t\t\t\t\t\t\t\t菜号\t\t菜名\t\t\t数量" +
                "\t\t花费\t\t\t餐桌号\t日期\t\t\t\t\t\t\t状态");
        List<Bill> bills = billService.getBills();
        for (Bill bill : bills) {
            System.out.println(bill);
        }
        System.out.println("==========================显示完毕===================");
    }

    //显示账单升级版(可以看到单价,多表查询)
    public void showBillsPlus(){
        System.out.println("==========账单如下=============");
        System.out.println("单号\t\t编号\t\t\t\t\t\t\t\t\t\t\t菜号\t\t菜名\t\t\t数量" +
                "\t\t花费\t\t\t餐桌号\t日期\t\t\t\t\t\t\t状态\t\t\t单价");
        List<MultiTableBean> billsPlus = multiTableBeanService.getBillsPlus();
        for (MultiTableBean plus : billsPlus) {
            System.out.println(plus);
        }
        System.out.println("==========================显示完毕===================");
    }
    //显示某个桌位的账单
    public void showBillsById(String tableId){
        System.out.println("==========账单如下=============");
        System.out.println("单号\t\t编号\t\t\t\t\t\t\t\t\t\t\t菜号\t\t菜名\t\t\t数量" +
                "\t\t花费\t\t\t餐桌号\t日期\t\t\t\t\t\t\t状态");
        List<Bill> bills = billService.getBills();
        for (Bill bill : bills) {
            if(bill.getTableId().equals(tableId)) System.out.println(bill);
        }
        System.out.println("==========================显示完毕===================");
    }
    //用于结账的方法
    public void payBill(){
        System.out.println("=========结账了=============");
        System.out.print("请输入您的餐桌号:");
        String tableId = scanner.next();
        DiningTable tb = diningTableService.getById(tableId);
        if(tb==null){
            System.out.println("失败,该餐桌不存在..");
        }else {
            if(diningTableService.getById(tableId)!=null){
                System.out.println("您的账单如下:");
                showBillsById(tableId);
                //确认是否真的需要结账
                System.out.print("请确认是否需要结账(如果账单都以结账请勿反复结账)(y/n)(请输入):");
                char yn = scanner.next().toUpperCase(Locale.ROOT).charAt(0);
                if(yn=='Y') {
                    System.out.print("请选择你的支付方式(1.微信/2.支付宝/3.现金):");
                    String mode = scanner.next();
                    System.out.println(billService.payBillByTableId(tableId,mode)?"结账成功":"结账失败");
                }else if(yn=='N') {
                    System.out.println("已取消~~~");
                }else {
                    System.out.println("输入有误..退出");
                }
            }else {
                System.out.println("失败,该餐桌没有账单....");
            }
        }
    }
    //显示主菜单的方法
    public void mainMeine(){
        //接收键盘输入
        String key = "";
        //控制是否退出菜单
        boolean exit=false;
        while (!exit){
            System.out.println("=============满汉楼============");
            System.out.println("\t\t 1.登录满汉楼");
            System.out.println("\t\t 9.退出满汉楼");
            System.out.print("请输入:");
            key=scanner.next();
            switch (key){
                case "1":
                    System.out.println("===========请登录==========");
                    System.out.print("输入id:");
                    String id = scanner.next();
                    System.out.print("输入密码:");
                    String pwd = scanner.next();
                    Employee emp = employeeService.getByIdAndPwd(id, pwd);
                    if(emp!=null){
                        System.out.println("=============登录成功===========");
                        //二级菜单
                        //控制是否退出登录
                        boolean exit0=false;
                        while (!exit0){
                            System.out.println("=============满汉楼二级菜单(欢迎"+emp.getName()+")============");
                            System.out.println("\t\t 1.显示餐桌状态");
                            System.out.println("\t\t 2.预定餐桌");
                            System.out.println("\t\t 3.显示所有菜品");
                            System.out.println("\t\t 4.点菜服务");
                            System.out.println("\t\t 5.查看账单");
                            System.out.println("\t\t 6.结账");
                            System.out.println("\t\t 9.退出登录");
                            System.out.print("请输入:");
                            key = scanner.next();
                            switch (key){
                                case "1":
                                    showTables();//显示餐桌信息
                                    break;
                                case "2":
                                    orderTable();//订餐
                                    break;
                                case "3":
                                    showMenus();//显示所有菜品
                                    break;
                                case "4":
                                   orderMenu();//点菜
                                    break;
                                case "5":
                                    showBills();//显示菜单
                                    showBillsPlus();//升级版
                                    break;
                                case "6":
                                    payBill();//结账
                                    break;
                                case "9":
                                    exit0 =true;
                                    System.out.println("==========退出登录===========");
                                    break;
                                default:
                                    System.out.println("====输入有误==========");
                                    break;
                            }
                        }
                    }else {
                        System.out.println("登录失败,请重试....");
                    }
                    break;
                case "9":
                    exit=true;
                    System.out.println("您已退出,感谢使用");
                    break;
                default:
                    System.out.println("输入错误");
                    break;
            }
        }
    }
}
